$().ready(function() {
	$('#register_form').validate({
	    rules: { 
	    	mobilePhone: {
	        required:true,
	        isMobile:true,
	        remote: {
	               url: "checkMobilePhone",
	               type: "post",
	               dataType: "json",
	               data: {
	            	   mobilePhone: function () {
	                       return $("#mobilePhone").val();
	                   }
	               },
	               dataFilter: function (data) {
	                   if (data == "false") {
	                       return true;
	                   }
	                   else {
	                       return false;
	                   }
	               }
	           }
	        },
	        password:{
	        required:true,
	        rangelength:[6,20]
	        },
	      	password_confirm:{
		    required:true,
		    equalTo: "#password"
		    },
	        SMSCode:{
	        	required:true,
	        	digits:true,
	        	remote: {
		               url: "checkRegisterSMSCode",
		               type: "post",
		               dataType: "json",
		               data: {
		            	   mobilePhone: function () {
		                       return $("#SMSCode").val();
		                   }
		               },
		               dataFilter: function (data) {
		                   if (data == "true") {
		                       return true;
		                   }
		                   else {
		                       return false;
		                   }
		               }
		           }
	        }
	    },
	    messages: {
	    	mobilePhone: {
		        isMobile: "请输入一个正确的手机号码",
		        remote: "该号码已注册，请直接登录"
		      },
		    password:{
			 },
			password_confirm:{
				equalTo: "请输入相同的密码"
			},
			SMSCode:{
				remote: "验证码输入不正确"
			}
	    }
	});
});

$(function(){
	/*绑定提交按钮事件*/
	$('#register_submit').on('click', function(){
		$.base64.utf8encode = true;
		var str = $("#mobilePhone").val()+":"+$('#password').val();
		var register_str = $.base64('encode', str);
		var $register_str = $('#register_str');
		$register_str.val(register_str);
	});
})

var wait = 60;

//点击验证码倒计时	
$('#button').on('click',function(){
	$.ajax({
		url : "sendRegisterSMS",
		dataType : "json",
		type : "POST",
		data : {
			"mobilePhone" : $("#mobilePhone").val()
		},
		success : function(data) {
			if (data==false) {
				alert("验证码发送异常，请检查手机号码是否输入正确")
            }
		},
		error : function() {
			alert("验证码发送异常，请检查手机号码是否输入正确")
		}
	});
	timeDown(this);
});


function timeDown(o){
	if (wait == 0) {
		o.removeAttribute("disabled");
		$(o).addClass('button-fill');
		o.value = "获取验证码";
		wait = 60;
	} else {
		o.setAttribute("disabled", true);
		$(o).removeClass('button-fill');
		o.value = "重新发送(" + wait + ")";
		wait--;
		setTimeout(function() {
			timeDown(o)
		}, 1000)
	}
}

